package com.da.web.dorm_repair.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.da.common.Result;
import com.da.web.dorm_repair.entity.DormRepair;
import com.da.web.dorm_repair.entity.RepairParm;
import com.da.web.dorm_repair.service.DormRepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author da
 * @time 2023/12/15 上午 8:27
 */
@RestController
@RequestMapping("/api/repair")
public class DormRepairController {
    @Autowired
    private DormRepairService dormRepairService;

    //新增报修信息
    @PostMapping
    public Result add(@RequestBody DormRepair dormRepair) {
        //设置报修时间
        dormRepair.setRepairTime(new Date());
        //设置维修状态
        dormRepair.setStatus("0");
        boolean save = dormRepairService.save(dormRepair);
        if (save) {
            return Result.success("报修成功");
        }
        return Result.error("报修失败");
    }

    //编辑
    @PutMapping
    public Result edit(@RequestBody DormRepair dormRepair) {
        boolean save = dormRepairService.updateById(dormRepair);
        if (save) {
            return Result.success("编辑成功");
        }
        return Result.error("编辑失败");
    }

    //删除
    @DeleteMapping("/{repairId}")
    public Result delete(@PathVariable("repairId") Long repairId) {
        boolean save = dormRepairService.removeById(repairId);
        if (save) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    //列表
    @GetMapping("/list")
    public Result list(RepairParm parm) {
        IPage<DormRepair> list = dormRepairService.pageList(parm);
        return Result.success("查询成功", list);
    }

    //获取已经居住的宿舍的名字
    @GetMapping("/getSelectBedNameList")
    public Result getSelectBedNameList() {
        List<String> list = dormRepairService.getSelectBedNameList();
        return Result.success("查询成功", list);
    }

}
